import { getCampusSelector } from '@/api/campus/campus'
import request from '@/utils/request'
import CustomForm from './Form/index.vue'
export default {
  components: { CustomForm },
  data() {
    return {
      showAll: false,
      query: {
        campusId: undefined,
        orderSource: undefined,
        orderType: undefined,
        status: undefined
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: ''
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [{ prop: 'code', label: '编码' }, { prop: 'name', label: '名称' }, { prop: 'type', label: '类别' }, { prop: 'unit', label: '单位' }, { prop: 'price', label: '单价' }, { prop: 'stock', label: '库存' }, { prop: 'num', label: '数量' }, { prop: 'isNomalVirtual', label: '虚拟商品' }, { prop: 'isMarketable', label: '是否上架' }, { prop: 'description', label: '备注' }, { prop: 'createUserId', label: '创建用户' }, { prop: 'createTime', label: '创建时间' }, { prop: 'updateUserId', label: '修改用户' }, { prop: 'updateTime', label: '修改时间' }],
      typeOptions: [],
      campusOptions: [],
      campusLoading: false,
      brandOptions: [],
      // 1-家校购买2-系统生成
      orderSourceOptions: [
        {
          enCode: '1',
          fullName: '家校购买'
        },
        {
          enCode: '2',
          fullName: '签约生成'
        }
      ],
      // 1-赠送2-购买
      orderTypeOptions: [
        {
          enCode: '1',
          fullName: '赠送'
        },
        {
          enCode: '2',
          fullName: '购买'
        }
      ],
      // 1-正常-退款
      orderStatusOptions: [
        {
          enCode: '1',
          fullName: '正常'
        },
        {
          enCode: '2',
          fullName: '退款'
        }
      ]
    }
  },
  computed: {},
  created() {
    this.initData()
    this.getTypeOptions()
  },
  methods: {
    expandVisible(row, rowIndex) {
      let data = row.row
      let res = []
      if (data.orderItems && data.orderItems.length > 0) {
        res.push('row-expand-has')
        return res
      } else {
        res.push('row-expand-cover')
        return res
      }
    },

    getTypeOptions() {
      this.$store.dispatch('base/getDictionaryData', { sort: 'GoodsType' }).then(res => {
        this.typeOptions = res
      })
      this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
        this.brandOptions = res
        this.query.brand = this.brandOptions[0].enCode
      })
    },
    async loadCampusOptions() {
      try {
        this.campusLoading = true
        const res = await getCampusSelector({ brand: this.query.brand, fields: 'id,fullName' })
        this.campusOptions = Array.isArray(res.data) ? res.data : []
      } catch (error) {
        console.error('加载校区下拉选项失败:', error)
        this.$message.error('校区选项加载失败')
        this.campusOptions = []
      } finally {
        this.campusLoading = false
      }
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/order`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/services/goods/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    handleMarketableChange(row) {
      let text = row.isMarketable ? '上架' : '下架'
      this.$confirm('确认要操作' + text + '商品吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.listLoading = true
          request({
            url: `/api/services/goods/change/${row.id}/${row.isMarketable}`,
            method: 'PUT'
          }).then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.listLoading = false
                this.search()
              }
            })
          })
        })
        .catch(() => {
          //取消
          row.isMarketable = row.isMarketable === true ? false : true
        })
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail)
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    }
  }
}
